// $Id: ListGenerator.java 45 2009-06-25 05:54:40Z pukkaone $
package com.googlecode.hessianorb;

import java.lang.reflect.Type;

/**
 * Generates C++ code for Hessian Map.
 */
public class MapGenerator extends AbstractClassGenerator {
    
    /** C++ container used to represent Hessian Map */
    private static final String CONTAINER = "std::map";
    
    /** header file for C++ container used to represent Hessian Map */
    private static final String CONTAINER_HEADER = "<map>";
    
//    private GeneratorFactory config;
    private Generator keyElementGenerator;
    
    private Generator valueElementGenerator;
    
//    private StringTemplateGroup group;
    
    /**
     * Constructor
     * 
     * @param config
     *            generator factory
     * @param elementType
     *            list element Java type
     */
    public MapGenerator(GeneratorFactory config, Type keyType, Type valueType) {
//        this.config = config;
        this.keyElementGenerator = config.getGenerator(keyType);
        this.valueElementGenerator = config.getGenerator(valueType);
        
//        group =  new StringTemplateGroup("listGroup");
    }

//    private StringTemplate getTemplate(String templateName, Headers headers) {
//        StringTemplate template = group.getInstanceOf(templateName);
//        template.setAttribute("headers", headers);
//        template.setAttribute("namespace", config.getNamespace());
//        return template;
//    }

//    private String capitalize(String name) {
//        return name.substring(0, 1).toUpperCase() + name.substring(1);
//    }
    
//    private String getHeaderFileBaseName() {
//        String cppElementType = elementGenerator.getCppType();
//        String[] parts = cppElementType.split("::");
//
//        // Skip the Enum part in the C++ enum mapped type name.
//        int i = parts.length - 1;
//        if (i > 0 && parts[i].equals("Enum")) {
//            --i;
//        }
//
////        return capitalize(parts[i]) + "List";
//        return capitalize(parts[i]);
//    }
    
//    private void generateHeader() {
//        String headerFileBaseName = getHeaderFileBaseName();
//        String cppElementType = elementGenerator.getCppType();
//        String cppContainerType = getCppType();
//
//        Headers headers = new Headers();
//        elementGenerator.includeHeader(headers); 
//        headers.addHeader(CONTAINER_HEADER);
//        
//        StringTemplate header = getTemplate("listHeader", headers);
//        header.setAttribute("guard", config.guardName(headerFileBaseName));
//        header.setAttribute("elementType", cppElementType);
//        header.setAttribute("containerType", cppContainerType);
//        
//        String code = header.toString();
//        config.writeHeaderFile(headerFileBaseName, code);
//    }

    @Override
    public String getCppType() {
        return CONTAINER + '<' + keyElementGenerator.getCppType() + "," 
        			+ valueElementGenerator.getCppType() + '>';
    }
    
    @Override
    public void includeHeader(Headers headers) {
//        headers.addHeader(config.headerFileName(getHeaderFileBaseName()));
    	keyElementGenerator.includeHeader(headers); 
    	valueElementGenerator.includeHeader(headers); 
        headers.addHeader(CONTAINER_HEADER);
    }
    
    @Override
    public void generate() {
//        generateHeader();
        keyElementGenerator.generate();
        valueElementGenerator.generate();
    }

	public String getDefaultValue() {		
		return null;
	}
}
